// pages/mine/guide/guide.js
const app = getApp(),
    globalData = app.globalData,
    api = require('../../../api/index'),
    utils = require('../../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        status: 0,
        attention: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.reload()
    },

    reload() {
        this.getPageNews();
    },

    async getPageNews() {
        let id = '2020082316172153574106'; // 新手指引 guide id
        try {
            let res = await api.news.getNews(id),
                data = res.data.ResData || {};
            console.log(res, '获取新手指引')
            this.setData({
                status: 2,
                attention: data.Description || ''
            })
            this.setImg();
        } catch (e) {
            console.log(e, '获取新手指引失败')
            this.setData({
                status: 1
            })
        }
    },

    // 富文本显示
    setImg() {
        if (!this.data.attention) {
            return;
        }

        let result = this.data.attention;
        const regex = new RegExp('<p', 'gi');
        const regexDiv = new RegExp('<div', 'gi');
        if (regex.test(this.data.attention)) {
            result = result.replace(regex, `<p style="width:100%;word-break:break-all;margin:0;padding:0;line-height:26px;font-size:14px;color:#6B6E75;"`);

            let result2 = result;
            const regex2 = new RegExp('<img', 'gi');
            if (regex2.test(result)) {
                result2 = result2.replace(regex2, `<img style="width:100%;display:block;margin:0 auto;padding:0;"`);

                let result3 = result2;
                const regex3 = new RegExp('style=""', 'gi');
                if (regex3.test(result2)) {
                    result3 = result3.replace(regex3, `alt`);
                    this.setData({
                        attention: result3
                    })
                } else {
                    this.setData({
                        attention: result2
                    })
                }
            } else {
                this.setData({
                    attention: result
                })
            }
        } else if (regexDiv.test(this.data.attention)) {
            result = result.replace(regexDiv, `<div style="margin:0;padding:0;width:100%;line-height:26px;font-size:14px;color:#6B6E75;"`);

            let result2 = result;
            const regex2 = new RegExp('<img', 'gi');
            if (regex2.test(result)) {
                result2 = result2.replace(regex2, `<img style="width:100%;margin:0;padding:0;"`);

                let result3 = result2;
                const regex3 = new RegExp('style=""', 'gi');
                if (regex3.test(result2)) {
                    result3 = result3.replace(regex3, `alt`);
                    this.setData({
                        attention: result3
                    })
                } else {
                    this.setData({
                        attention: result2
                    })
                }
            } else {
                this.setData({
                    attention: result
                })
            }
        }

        // console.log(this.data.attention, 'attention')
    },
})